<!--
    设备分类
 -->
<template>
    <div class="page">
        <div class="nuclein-query">
            <div>设备分类</div>
            <div style="margin-left: auto;">
                <!-- <el-button type="primary" @click="handlerAdd()">添加</el-button> -->
            </div>
        </div>
        <div class="device-type">
            <p>
                <span>设备类型：</span>
                <el-select size="mini" v-model="type" placeholder="请选择" @change="handlerChange" clearable>
                    <el-option v-for="item in deviceLogType" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </p>
        </div>
        <div class="content-row">
            <el-table :data="tableData" style="width: 100%" border>
                <el-table-column label="设备类型名称" prop="deviceType" align="center" min-width="140">
                </el-table-column>
                <el-table-column label="设备数量" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success">
                            {{ scope.row.deviceCount }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="在线数量" align="center">
                    <template slot-scope="scope">
                        <el-tag type="succes'">
                            {{ scope.row.onlineNumber }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160">
                    <template slot-scope="scope">
                        <el-button size="mini" page="succes" @click="handleQuery(scope.row)">查看详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- // 分页 -->
        <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 30, 40]" :page-size="size" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        <!-- 抽屉弹窗 -->
        <el-drawer title="设备详情信息" :visible.sync="drawer" direction="rtl" size="60%">
            <div class="device-type">
                <p>
                    <span>设备状态：</span>
                    <el-select size="mini" v-model="onlineState" placeholder="请选择" @change="handlerDrawerChange" clearable>
                        <el-option v-for="item in deviceStatusList" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </p>
            </div>
            <div class="drawer-table">
                <el-table :data="tableList" border>
                    <el-table-column property="deviceName" label="设备名称"></el-table-column>
                    <el-table-column property="deviceSite" label="设备地址"></el-table-column>
                    <el-table-column property="createTime" label="时间"></el-table-column>
                    <el-table-column property="deviceSn" label="设备编号"></el-table-column>
                    <el-table-column property="deviceState" label="设备状态">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.deviceState == 0 ? 'danger' : 'success'">
                                {{ scope.row.deviceState == 0 ? '离线' : '在线' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination">
                <el-pagination @size-change="handleDrawerSize" @current-change="handleDrawerPage"
                    :current-page="drawerPage.page" :page-sizes="[5, 10, 20, 30, 40]" :page-size="drawerPage.limit"
                    layout="total, sizes, prev, pager, next, jumper" :total="drawerPage.total">
                </el-pagination>
            </div>
        </el-drawer>
    </div>
</template>

<script>
import { management, getOneList } from '../../api/api'
import commonFunction from '../../utils/commonFunction'
import commonData from '../../utils/commonData'
export default {

  data () {
    return {
      drawer: false,
      deviceLogType: commonData.equipmentTypeList,
      deviceStatusList: [
        { label: '全部', value: null },
        { label: '离线', value: 0 },
        { label: '在线', value: 1 }
      ],
      total: 10,
      page: 1,
      size: 10,
      formLabelWidth: '160px',
      tableData: [],
      tableList: [],
      type: 0,
      deviceType: '',
      total: 0,
      onlineState: null,
      drawerPage: {
        page: 1,
        limit: 5,
        total: 0,
        deviceType: ''
      }
    }
  },
  created () {
    this.gitList()
  },
  methods: {
    // 获取
    gitList () {
      const data = {
        type: this.type,
        page: this.page,
        size: this.size
      }
      management({ ...data }).then(res => {
        if (res.code == 0) {
          const { records, total } = res.page
          this.total = total
          this.tableData = records
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handlerChange (e) {
      this.type = e
      this.gitList()
    },
    handlerDrawerChange () {
      this.handleQuery(this.drawerPage)
    },
    handleQuery (row) {
      this.drawerPage.deviceType = row.deviceType ? row.deviceType : ''
      this.drawer = true
      const data = {
        type: this.type,
        page: this.drawerPage.page,
        size: this.drawerPage.limit,
        deviceType: row.deviceType,
        onlineState: this.onlineState
      }
      getOneList({ ...data }).then(res => {
        if (res.code == 0) {
          const { total, records } = res.page
          this.drawerPage.total = total
          this.tableList = records.map(item => {
            const createTime = commonFunction.getTimeFormat(+new Date(item.createTime), 'YYYY-MM-DD hh:mm:ss')
            return {
              ...item,
              createTime
            }
          })
        } else if (res.msg == '没有该类型') {
          this.$message.error(res.msg)
          this.tableList = []
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 分页
    handleSizeChange (val) {
      this.size = val
      this.gitList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.gitList()
    },
    // 抽屉分页
    handleDrawerSize (val) {
      this.drawerPage.limit = val
      this.handleQuery(this.drawerPage)
    },
    handleDrawerPage (val) {
      this.drawerPage.page = val
      this.handleQuery(this.drawerPage)
    }
  }
}
</script>

<style scoped>
.page {
    height: 100%;
}

.nuclein-query {
    display: flex;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #DCDFE6;
}

.content-row {
    overflow: hidden;
}

.device-type {
    padding: 20px 20px 10px 20px;
}

/* 分页 */
.pagination {
    width: 100%;
    padding: 20px;
    border-top: 1px solid #DCDFE6;
}

.drawer-table {
    padding: 20px;
}
</style>
